package cn.wolfcode.hander;

import cn.wolfcode.domain.OrderInfo;
import cn.wolfcode.redis.SeckillRedisKey;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

@Component
@Slf4j
@CanalTable(value = "t_order_info")
public class OrderInfoHander implements EntryHandler<OrderInfo> {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public void insert(OrderInfo orderInfo) {
        System.out.println("插入数据");
        //在redis当中存入数据为了重复下单做铺垫,下单就存进redis下次查出来就不能下单了
        redisTemplate.opsForSet().add(SeckillRedisKey.SECKILL_ORDER_SET.getRealKey(orderInfo.getSeckillTime()+""),orderInfo.getUserId()+":"+orderInfo.getSeckillId());
        //把订单放到redis中
        redisTemplate.opsForHash().put(SeckillRedisKey.SECKILL_ORDER_HASH.getRealKey(""),orderInfo.getOrderNo(), JSON.toJSONString(orderInfo));
        System.out.println("订单插入,同步到redis");
    }



    @Override
    public void update(OrderInfo before, OrderInfo after) {
        System.out.println("修改数据");
        //musql改redis也要修改
        redisTemplate.opsForHash().put(SeckillRedisKey.SECKILL_ORDER_HASH.getRealKey(""),after.getOrderNo(), JSON.toJSONString(after));
        System.out.println("数据进行修改同步到redis中");
    }

    @Override
    public void delete(OrderInfo orderInfo) {
        System.out.println("删除数据");
    }
}
